package xcmg.device.web.threedayplan;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.threedayplan.ThreeDayPlanService;
import xcmg.device.service.vo.threedayplan.DlvInfoQueryVO;
import xcmg.device.service.vo.threedayplan.ThreeDayPlanQueryVO;
import xcmg.device.service.vo.threedayplan.ThreeDayPlanResultVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;
import java.util.Date;

/**
 * Created by Administrator on 2019/8/12.
 * 三日计划入口
 */
@RestController
@RequestMapping("/threeDayPlan")
@Api("三日计划入口")
public class ThreeDayPlanController extends BasicController {

    @Autowired
    private ThreeDayPlanService threeDayPlanService;

    @PostMapping("/add")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("新增")
    public ActionResult add(@RequestBody ThreeDayPlanResultVO threeDayPlanResultVO){
        if (null == threeDayPlanResultVO || null == threeDayPlanResultVO.getDetailVOList()
                || threeDayPlanResultVO.getDetailVOList().isEmpty()){
            return actionResult(ErrorCode.IllegalArument);
        }
        try{
            threeDayPlanResultVO.setOrgId(getOrgId());
            threeDayPlanResultVO.setCreateDate(new Date());
            threeDayPlanResultVO.setCreateId(getUserId());
            threeDayPlanResultVO.setCreateName(getUserName());
            return actionResult(threeDayPlanService.add(threeDayPlanResultVO));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(),e.getMessage());
        }
    }

    @PostMapping(value = "/remove")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("删除")
    public ActionResult delete(@RequestBody String[] ids) throws Exception {
        if (ids == null || ids.length == 0) {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(threeDayPlanService.del(Arrays.asList(ids)));
    }

    @PostMapping(value = "/update")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("修改")
    public ActionResult update(@RequestBody ThreeDayPlanResultVO threeDayPlanResultVO) throws Exception {
        if (threeDayPlanResultVO == null || threeDayPlanResultVO.getDetailVOList() == null || threeDayPlanResultVO.getDetailVOList().isEmpty()) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try{
            return actionResult(threeDayPlanService.update(threeDayPlanResultVO,getUserId(),getUserName()));
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(),e.getMessage());
        }
    }

    @GetMapping(value = "/detail")
    @ApiOperation("获取三日计划详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult detail(@RequestParam("id") String id) throws Exception {
        if (StringUtils.isBlank(id)) {
            return actionResult(ErrorCode.IllegalArument);
        }

        return actionValueResult(threeDayPlanService.detail(id));
    }

    @PostMapping(value = "/list")
    @ApiOperation("获取三日计划列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<ThreeDayPlanQueryVO> condition) throws Exception {
        ThreeDayPlanQueryVO threeDayPlanQueryVO = condition.getFilters();
        if (null == threeDayPlanQueryVO) {
            threeDayPlanQueryVO = new ThreeDayPlanQueryVO();
        }
        threeDayPlanQueryVO.setOrgId(getOrgId());
        condition.setFilters(threeDayPlanQueryVO);
        PageCommonVO pageCommonVO = threeDayPlanService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @PostMapping(value = "/getAddList")
    @ApiOperation("获取三日计划新增列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult getAddList(@RequestBody SearchCommonVO<DlvInfoQueryVO> condition) throws Exception {
        DlvInfoQueryVO filters = condition.getFilters();
        if (null == filters) {
            return actionResult(ErrorCode.IllegalArument);
        }
        filters.setOrgId(getOrgId());
        PageCommonVO pageCommonVO = threeDayPlanService.getAddList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

}
